package com.miaosha.fx;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.log4j.Logger;

import com.miaosha.fx.jsjava.Js2Java;

import netscape.javascript.JSObject;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * 使用webview作为界面
 * @author TANGJZ2
 *
 */
public class MiaoShaWebview extends Application{
	public static Logger logger = Logger.getLogger(MiaoShaWebview.class);
	
	public static void main(String[] args) {  
        Application.launch(MiaoShaWebview.class, args);  
    }  

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(new Browser());
//        stage.setTitle("美的内购一折券秒杀程序");
        stage.setScene(scene);
        stage.show ();
	}
	
	
	class Browser extends Region {
	    private WebView browser = new WebView();
	    private WebEngine webEngine = browser.getEngine();
	    public Browser() {
	    	browser.setId("browser");
	        getStyleClass().add("browser");
	        String filePath = "file:///" + getClass().getResource("/index.html").getPath();
//	        String filePath = "file:///D:/miaosha/index.html";
	        logger.info("filePath = " + filePath);
	        webEngine.load(filePath); 
//	        webEngine.load("https://www.baidu.com/");
	        getChildren().add(browser);
	        JSObject win = (JSObject) webEngine.executeScript("window");
	        win.setMember("js2Java", new Js2Java(webEngine));
	    }
	    
	    
	    private Node createSpacer() {
	        Region spacer = new Region();
	        HBox.setHgrow(spacer, Priority.ALWAYS);
	        return spacer;
	    }
	 
	    @Override 
	    protected void layoutChildren() {
	        double w = getWidth();
	        double h = getHeight();
	        layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
	    }
	 
	    @Override 
	    protected double computePrefWidth(double height) {
	        return 1000;
	    }
	 
	    @Override 
	    protected double computePrefHeight(double width) {
	        return 500;
	    }
	    
	}
	
   
}
